package com.ddpt.permission.dubbo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.enums.MerchantServiceType;
import com.lh.center.common.exception.CommonException;
import com.lh.center.common.exception.ErrorEnum;
import com.lh.center.permission.api.dubbo.PermissionService;
import com.lh.center.permission.api.entity.Permission;
import com.lh.center.permission.api.request.PermissionQueryRequest;
import com.lh.center.permission.api.session.MerchantPermission;
import com.lh.center.permission.api.session.SessionPermission;
import com.lh.center.permission.mapper.PermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@DubboService
public class DubboPermissionServiceImpl implements PermissionService{

    @Autowired
    private com.lh.center.permission.service.PermissionService permissionService;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void addNew(Permission permission) {
        log.info("添加参数:{}", JSONUtil.toJsonStr(permission));
        if(null == permission.getId()) {
            if(null == permission.getParentId()) {
                permission.setParentId(0L);
            }
        } else {
            permission.setParentId(null);
            permission.setAdminService(null);
            permission.setMerchantService(null);
        }
        permissionService.saveOrUpdate(permission);
    }

    @Override
    public List<SessionPermission> queryTree(PermissionQueryRequest queryRequest) {
        return permissionService.queryTree(queryRequest);
    }

    @Override
    public void deletePermission(PermissionQueryRequest queryRequest) {
        Long id = queryRequest.getId();
        if(permissionService.count(Wrappers.lambdaQuery(Permission.class).eq(Permission::getParentId,id)) > 0) {
            throw new CommonException(ErrorEnum.PERMISSION_NOT_EMPTY);
        }
        permissionService.removeById(id);
    }

    @Override
    public List<MerchantPermission> queryNormalPermissionTree(List<MerchantServiceType> serviceList) {
        List<SessionPermission> permissionList = permissionMapper.queryNormalSessionPermission(serviceList);
        Map<MerchantServiceType, List<SessionPermission>> permissionMap = permissionList.stream().collect(Collectors.groupingBy(SessionPermission::getMerchantService));
        List<MerchantPermission> permissions = new ArrayList<>();
        MerchantPermission cur;
        for(MerchantServiceType service : serviceList) {
            cur = new MerchantPermission();
            cur.setServiceType(service);
            cur.setPermissionLayer(permissionService.buildLayerPermission(permissionMap.get(service)));
            cur.setPermissionList(null);
            permissions.add(cur);
        }
        return permissions;
    }
}
